﻿using AddressBook.Model.EF;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AddressBook
{
    internal class AddressBookServices
    {
        /// <summary>
        /// 获得所有数据
        /// </summary>
        /// <returns></returns>
        public List<T001Address> GetAll()
        {
            var db = new AddressBook2024Entities();
            return db.T001Address.ToList();
        }
        /// <summary>
        /// 新建记录
        /// </summary>
        /// <param name="model"></param>
        public void Create(T001AddressBookCreateModel model)
        {
            var db = new AddressBook2024Entities();
            var newItem = new T001Address()
            {
                Id = Guid.NewGuid(),
                Name = model.Name,
                PhoneNo = model.PhoneNo,
                Email = model.Email,
                Memo = model.Memo,
            };
            db.T001Address.Add(newItem);
            db.SaveChanges();
        }
        /// <summary>
        /// 根据Id，获得指定记录
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public T001Address GetById(Guid id)
        {
            var db = new AddressBook2024Entities();
            return db.T001Address.Find(id);
        }

        /// <summary>
        /// 修改记录
        /// </summary>
        /// <param name="model"></param>
        public void Edit(T001AddressBookEditModel model)
        {
            var db = new AddressBook2024Entities();
            var item = db.T001Address.Find(model.Id);
            if (item != null)
            {
                item.Name = model.Name;
                item.PhoneNo = model.PhoneNo;
                item.Email = model.Email;
                item.Memo = model.Memo;
            }
            db.SaveChanges();
        }
        /// <summary>
        /// 删除记录
        /// </summary>
        /// <param name="id"></param>
        public void Delete(Guid id)
        {
            var db = new AddressBook2024Entities();
            var item = db.T001Address.Find(id);
            if (item != null)
            {
                db.T001Address.Remove(item);
                db.SaveChanges();
            }
        }
    }
}
